package com.sqx.modules.userCertification.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqx.common.utils.DateUtils;
import com.sqx.common.utils.Result;
import com.sqx.modules.app.entity.UserEntity;
import com.sqx.modules.app.service.UserService;
import com.sqx.modules.common.entity.CommonInfo;
import com.sqx.modules.common.service.CommonInfoService;
import com.sqx.modules.message.entity.MessageInfo;
import com.sqx.modules.message.service.MessageService;
import com.sqx.modules.ruleClassify.entity.RuleClassify;
import com.sqx.modules.userCertification.entity.UserCertification;
import com.sqx.modules.userCertification.dao.UserCertificationDao;
import com.sqx.modules.userCertification.service.IUserCertificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sqx.modules.userCertification.vo.UserCertificationVo;
import com.sqx.modules.utils.SenInfoCheckUtil;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 实名认证表 服务实现类
 * </p>
 *
 * @author wuchang
 * @since 2023-07-25
 */
@Service
public class UserCertificationServiceImpl extends ServiceImpl<UserCertificationDao, UserCertification> implements IUserCertificationService {

    @Autowired
    private UserService userService;
    @Autowired
    private CommonInfoService commonInfoService;
    @Autowired
    private MessageService messageService;

    @Override
    public Result saveUserCertification(UserCertification userCertification) {
        UserEntity userEntity = userService.getById(userCertification.getUserId());

        if (userEntity == null) {
            return Result.error("用户不存在");
        }
        userCertification.setExamineStatus(0);
        QueryWrapper<UserCertification> wrapper = new QueryWrapper<UserCertification>().eq("user_id", userCertification.getUserId());
        if (userCertification.getUserType() == 1) {
            wrapper.eq("id_card", userCertification.getIdCard());
        } else {
            wrapper.eq("registration_no", userCertification.getRegistrationNo());
        }
        if (userCertification.getCertificationId() != null) {
            wrapper.ne("certification_id", userCertification.getCertificationId());
        }
        UserCertification one = baseMapper.selectOne(wrapper);
        if (one != null) {
            if (userCertification.getUserType() == 1) {
                return Result.error("当前身份证号已被使用");
            } else {
                return Result.error("当前企业认证已存在");
            }
        }
        userEntity.setCheckCertification(0);
        userService.updateById(userEntity);
        if (userCertification.getCertificationId() != null) {
            return Result.upStatus(baseMapper.updateById(userCertification));
        } else {
            userCertification.setCreateTime(LocalDateTime.now());
            return Result.upStatus(baseMapper.insert(userCertification));
        }
    }

    @Override
    public Result examine(Long certificationId, Integer examineStatus, String examineDetails) {

        UserCertification userCertification = baseMapper.selectById(certificationId);
        if (userCertification == null) {
            return Result.error("实名认证不存在");
        }
        userCertification.setExamineStatus(examineStatus);
        userCertification.setExamineDetails(examineDetails);
        UserEntity userEntity = userService.getById(userCertification.getUserId());
        if (examineStatus == 1) {
            userEntity.setCheckCertification(1);
            userEntity.setUserType(userCertification.getUserType());
        } else {
            userEntity.setCheckCertification(2);
        }
        userService.updateById(userEntity);
        DateTimeFormatter fmt = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        CommonInfo one = commonInfoService.findOne(67);
        List<String> msgList = new ArrayList<>();
        msgList.add(0, "实名认证");
        msgList.add(1, userCertification.getCreateTime().format(fmt));
        if (examineStatus == 1) {
            msgList.add(2, "通过");
        } else if (examineStatus == 2) {
            msgList.add(2, "未通过");
        }
        if (examineDetails.length() >= 20) {
            examineDetails = examineDetails.substring(0, 15) + "...";
        }
        msgList.add(3, DateUtils.format(new Date()));
        msgList.add(4, examineDetails);
        if (StringUtils.isNotBlank(userEntity.getOpenId())) {
            SenInfoCheckUtil.sendMsg(userEntity.getOpenId(), one.getValue(), msgList, 1, null);
        }
        if (StringUtils.isNotBlank(userEntity.getClientid())) {
            userService.pushToSingle("实名认证审核结果", examineDetails, userEntity.getClientid());
        }
        MessageInfo messageInfo = new MessageInfo();
        messageInfo.setTitle("审核通知");
        if ("1".equals(examineDetails)) {
            messageInfo.setContent("您的实名认证审核已通过");
        } else if ("2".equals(examineDetails)) {
            messageInfo.setContent("您的实名认证审核未通过审核,可点击实名认证详情查看原因");
        }
        messageInfo.setState(String.valueOf(5));
        messageInfo.setUserId(String.valueOf(userEntity.getUserId()));
        messageInfo.setCreateAt(DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
        messageInfo.setIsSee("0");
        messageService.save(messageInfo);
        return Result.upStatus(baseMapper.updateById(userCertification));

    }

    @Override
    public IPage<UserCertification> getCertificationList(Integer page, Integer limit, UserCertificationVo userCertificationVo) {
        Page<UserCertification> pages;
        if (page != null && limit != null) {
            pages = new Page<>(page, limit);
        } else {
            pages = new Page<>();
            pages.setSize(-1);
        }

        return baseMapper.getCertificationList(pages, userCertificationVo);
    }

    @Override
    public Result deleteCertification(Long certificationId) {
        UserCertification certification = baseMapper.selectById(certificationId);
        if (certification != null) {
            UserEntity userEntity = userService.getById(certification.getUserId());
            if (userEntity != null) {
                userService.setUserCheckCertificationNull(userEntity.getUserId());
            }
        }
        return Result.upStatus(baseMapper.deleteById(certificationId));
    }
}
